package com.site.springboot.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.site.springboot.core.dao.AdminMapper;
import com.site.springboot.core.entity.Admin;
import com.site.springboot.core.service.AdminService;
import com.site.springboot.core.util.MD5Util;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
@Service
public class AdminServiceImpl implements AdminService {
    @Resource
    private AdminMapper adminMapper;

    @Override
    public Admin login(String userName, String password) {
        String passwordMd5 = MD5Util.MD5Encode(password, "UTF-8");
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name", userName)
                .eq("login_password", passwordMd5);
        return adminMapper.selectOne(queryWrapper);
    }

    @Override
    public Admin getUserDetailById(Long loginUserId) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_id", loginUserId);
        return adminMapper.selectOne(queryWrapper);
    }

    @Override
    public Boolean updatePassword(Long loginUserId, String originalPassword, String newPassword) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_id", loginUserId);
        Admin adminUser = adminMapper.selectOne(queryWrapper);
        //当前用户非空才可以进行更改
        if (adminUser != null) {
            String originalPasswordMd5 = MD5Util.MD5Encode(originalPassword, "UTF-8");
            String newPasswordMd5 = MD5Util.MD5Encode(newPassword, "UTF-8");
            //比较原密码是否正确
            if (originalPasswordMd5.equals(adminUser.getLoginPassword())) {
                //设置新密码并修改
                adminUser.setLoginPassword(newPasswordMd5);
                UpdateWrapper<Admin> updateWrapper = new UpdateWrapper<>();
                updateWrapper.set("login_password", adminUser.getLoginPassword())
                        .and(id->id.eq("admin_id", adminUser.getAdminId()));
                if (adminMapper.update(adminUser,updateWrapper) > 0) {
                    //修改成功则返回true
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public Boolean updateName(Long loginUserId, String loginUserName, String nickName) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_id", loginUserId);
        Admin adminUser = adminMapper.selectOne(queryWrapper);
        //当前用户非空才可以进行更改
        if (adminUser != null) {
            //设置新密码并修改
            adminUser.setLoginName(loginUserName);
            adminUser.setAdminNickName(nickName);
            UpdateWrapper<Admin> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set(loginUserName!=null,"login_name", adminUser.getLoginName())
                    .set(nickName!=null,"admin_nick_name", adminUser.getAdminNickName())
                    .and(id->id.eq("admin_id", adminUser.getAdminId()));
            if (adminMapper.update(adminUser,updateWrapper) > 0) {
                //修改成功则返回true
                return true;
            }
        }
        return false;
    }

}
